package com.example.stumanager.database;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

import java.util.ArrayList;

/**
 * 留言数据库操作类
 */
public class MessageSql extends SQLiteOpenHelper {
    public MessageSql(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }
    private SQLiteDatabase dp;
    public MessageSql(Context context){
        super(context,"Comment",null,1);
        dp=this.getWritableDatabase();
    }
    public boolean add(String g){
        ContentValues values=new ContentValues();
        values.put("content",g );
        long i=dp.insert("comment",null,values);
        if(i>0){
            Log.d("","插入成功");
            return true;
        }
        return false;
    }

    public ArrayList getAll(){
        ArrayList array=new ArrayList();
        Cursor cursor=dp.query("comment",null,null,null,null,null,null);
        while(cursor.moveToNext()){
            @SuppressLint("Range") String content=cursor.getString( cursor.getColumnIndex("content"));
            array.add(content);
        }
        return array;
    }
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String sql="create table comment( content text not null )";
        sqLiteDatabase.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}
